from django.db.models import Q
from django.shortcuts import render
from course import courseser
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination

from course import models, courseser
from rest_framework.views import APIView
from rest_framework.response import Response


# Create your views here.
class HomeCate(APIView):
    def get(self, request):
        catelist = models.CategoryModel.objects.filter(recommend=True, parent=None).order_by('order')
        ser = courseser.CategorySerializer(catelist, many=True)
        data = ser.data
        for item in data:
            item["son"] = item["son"][:3]
        return Response({'msg': 'ok', 'data': ser.data})


class CateCourse(APIView):
    def get(self, request):
        parentid = request.query_params.get('pid')
        catelist = models.CategoryModel.objects.filter(id=parentid).first()
        ser = courseser.CategorySerializer(catelist)
        sonlist = []
        for item in ser.data["son"]:
            sonlist.append(item["id"])
        homecourse = models.CourseModel.objects.filter(category_id__in=sonlist).order_by('-order')[0:4]
        coursedata = courseser.CourseSerializer(homecourse, many=True).data
        datalist = ser.data
        datalist["course"] = coursedata
        return Response({'msg': 'ok', 'data': datalist})

class GetParentCate(APIView):
    def get(self, request):
        parentcate = models.CategoryModel.objects.filter(parent=None)
        ser = courseser.CategorySerializer(parentcate, many=True)
        return Response({'msg': 'ok', 'data': ser.data})

class GetSonCate(APIView):
    def get(self, request):
        parentid = request.query_params.get('pid')
        if parentid:
            sondata = models.CategoryModel.objects.filter(parent=parentid)
        else:
            sondata = models.CategoryModel.objects.filter(parent__gt=0)
        ser = courseser.CateSerializer(sondata, many=True)
        return Response({'msg': 'ok', 'data': ser.data})



class PageCourse(PageNumberPagination):
    page_size = 3
    page_size_query_param = 'size'
    page_query_param = 'page'



class GetCourse(ListAPIView):
    serializer_class = courseser.CourseSerializer
    pagination_class = PageCourse

    # def get_queryset(self):
    #     queryset = models.CourseModel.objects.filter(isshow=True).order_by('order')
    #     #获取到前端传过来的参数
    #     cateid = self.request.query_params.get('cateid')
    #     directionid = self.request.query_params.get('directionid')
    #     if cateid:
    #         #如果cateid不为空，就不用管 directionid 只是检索 cateid
    #         queryset = models.CourseModel.objects.filter(category=cateid,isshow=True).order_by('order')
    #     else:
    #         #如果cateid为空，directionid 不为空
    #         if directionid:
    #             #根据directionid 拿到所有的二级分类  [son1,son2,son3]
    #             son = models.CourseModel.objects.filter(parent=directionid)
    #             sonlist = []
    #             for i in son:
    #                 sonlist.append(i.id)
    #             #根据二级分类的id 拿到所有的课程
    #             queryset = models.CourseModel.objects.filter(category__in=sonlist,isshow=True).order_by('order')
    #     return queryset

    def get_queryset(self):
        query=Q(isshow=True)
        #获取到前端传过来的参数
        cateid = self.request.query_params.get('cateid')
        directionid = self.request.query_params.get('directionid')
        level = self.request.query_params.get('level')
        orderdata = self.request.query_params.get('ordering')
        if level:
            query = query & Q(level=level)
        if cateid:
            query = query & Q(category=cateid)
        else:
            if directionid:
                #根据directionid 拿到所有的二级分类  [son1,son2,son3]
                son = models.CourseModel.objects.filter(parent=directionid)
                sondata = []
                for i in son:
                    sondata.append(i.id)
                query = query & Q(category__in=sondata)
        if orderdata:
            queryset = models.CourseModel.objects.filter(query).order_by("order",orderdata)
        else:
            queryset = models.CourseModel.objects.filter(query).order_by('order')
        return queryset



class GetDetail(APIView):
    def get(self,request):
        id=request.query_params.get("id")
        data=models.CourseModel.objects.filter(id=id).first()
        ser=courseser.CourseSerializer(data)
        return Response({"msg":"success","data":ser.data})
